/**
 * @Description 基础框架服务调用实例-全文检索服务
 * @Author  guoyanfei
 * @date 2018/03/28
 */
var serviceUrl = '/gdp'
var searchServices=function () {
    //检索数据
    var getsearch ={
        getData:function (pageindex,isPage) {
            var searchUrl=serviceUrl + '/business/searchServer/seachDataHighlight';
            $.ajax({
                url: searchUrl,
                type: 'get',
                async: false,
                data: { serviceID: serviceId,keyword:$("#searchParm").val(),pageNO:pageindex,pageSize:20,includesContext:true},
                success: function (res) {
                    if(res.result==0){
                        if(isPage){
                            pages.generatePages(res.data.total);
                        }
                        $("#infomess").html(res.data.description+",共约"+res.data.total+"条记录");
                        var result =JSON.stringify(res.data.rows)
                            .replace(/},/g,"},<br/>");

                        $("#info").html(result);
                    }else {
                        $("#info").html(JSON.stringify(res));
                    }

                },
                error: function () {
                    bootbox.alert("检索数据失败!");
                }
            });
        },
        getkeyword:function () {
            //查询热词
            $.ajax({
                type:'get',
                url:serviceUrl + '/business/searchServer/seachHotword',
                data:{serviceID:serviceId},
                async:false,
                success:function (res) {
                    $("#kwul").html("");
                    $.each(res,function (i,e) {
                        $("#kwul").append("<li data-ve='"+e+"'>"+e+"</li>");
                    })
                }
            });
        },
        init:function () {
            $("#btn_search").click(function () {
                getsearch.getData(1,true);
                $("#keyword").css("display","none");
            });
            $("#searchParm").focus(function () {
                getsearch.getkeyword();
                $("#keyword").css("display","block");
            });

            $("#kwul").on('click','li',function () {
                $("#searchParm").val($(this).attr("data-ve"));
                $("#keyword").css("display","none");
            });
        }

    };

    //分页
    var pages = {
        generatePages:function (total) {//生成分页按钮
            $("#page").html("");
            $('#page').jqPaginator({
                totalPages: total,
                visiblePages: 20,
                currentPage: 1,
                first: '<li class="first"><a href="javascript:;">首页</a></li>',
                prev: '<li class="prev"><a href="javascript:;">上一页</a></li>',
                next: '<li class="next"><a href="javascript:;">下一页</a></li>',
                last: '<li class="last"><a href="javascript:;">末页</a></li>',
                page: '<li class="page"><a href="javascript:;">{{page}}</a></li>',
                onPageChange: function (num, type) {
                    getsearch.getData(num,false);
                }
            });
        }
        
    };

    return{
        init:function () {
            getsearch.init();
        }
    }
}();

//服务初始化
$(function () {
    $("#sp_seviceUrl").append("服务地址：" + serviceUrl + "/business/singleEditServer/singleAdd");
    $("#sp_seviceUrl").append("&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;服务ID：" + serviceId);
    $("#show_serviceUrl").append(serviceUrl);
    $("#show_serviceId").append(serviceId);
    searchServices.init();
})
